Arrange objects based on list

Hello,

I am fairly new to DXL and thus running in to some trouble. I have a doors module with few thousand objects which I wish to rearrange based on a list of object headings. Here is my attempt, however it does not work as expected.

pragma runLim, 0

Module m = current
Object o
Object oPrev = first(m)

int index = 0
string str
Skip fileLines = create

// Choose files and read it
string filepath = "C:\\Users\\USERNAME\\Desktop\\list.txt"    // list containing headings
Stream input = read(filepath)

while (true)
{
    input >> str
    if(end of input) break
    put(fileLines, index++, str)
}

for o in m do
{
        string CM = o."Code Moved"
        if (CM == "")
        {
                string OH = o."Object Heading"
                for str in fileLines do
                {
                        if ((OH == str))
                        {
                                if (o != oPrev)
                                {
                                        move(o,oPrev)
                                        o."Code Moved" = "Yes"
                                        oPrev = o
                                }
                        }
                }
        }
}

The  "List.txt" file contains object headings, some what like this:

Customer Documents
Government Documents
Industry Documents
Supplier Documents

 


DOORS_user - Thu Sep 17 15:13:54 EDT 2015

Re: Arrange objects based on list
pommCannelle - Mon Sep 21 11:35:17 EDT 2015

Hi ! 

Perhaps the link with the objects Customer/Government/Industry/Supplier is missing somewhere. ;)
I think you should create the 4 object with your titles ... Then loop and move your objects at the right place ....

Perhaps try to replace the loop on the module with something like ... 

// get the ref objects
Skip skp_ref = createString()
for o in m do {
    if (! find(fileLines, probeAttr_(o, "object heading")) ) continue
    put(skp_ref,probeAttr_(o, "object heading"), o)
}
// do the move part ;)
Object oref = null
for o in m do {
    if ( !find(skp_ref, probeAttr_(o,"Object Heading"), oref) ) continue
    move(o, last below oref)
    o."Code Moved" = "Yes"
}
// clean the memory ... 
delete fileLines
delete skp_ref

I have no DOORS client to test it, but this is the idea ;)

Post your code if you are in trouble !

Re: Arrange objects based on list
llandale - Sat Sep 26 17:41:12 EDT 2015

Boring preamble: =================

Every programmer knows not to do this, as is screws up the loop.

  • for I in whatever
  • {  sometimes modify I
  • }

You are doing the same thing when you modify the order of objects from within the obj loop.  Consider the following:

  • for o in m do
  • {  move o so it is the last object in the module
  • }

That loop works only on one object at all, since when the loop goes back and tries to find the "next" object, there isn't one since the 1st object is now the last one in the module.

This is true for (?all?) loops of the form "for SomethingA in SomethingB" like "for link in object".

You need to 1st stage the objects in a skip list, THEN start moving them around.

==================================

  • Skip skpObjects = create() // KEY and DATA both 'Object'
  • for o in m do
  • {  put(skpObjects, o, o)
  • }
  • for o in skpObjects do
  • {  proceed with your algorithm
  • }

In your case you may have other problems since you are moving an object after another object, and I wonder what happens if you subsequently move THAT object.

-Louie

Re: Arrange objects based on list
DOORS_user - Mon Sep 28 08:20:16 EDT 2015

llandale - Sat Sep 26 17:41:12 EDT 2015

Boring preamble: =================

Every programmer knows not to do this, as is screws up the loop.

  • for I in whatever
  • {  sometimes modify I
  • }

You are doing the same thing when you modify the order of objects from within the obj loop.  Consider the following:

  • for o in m do
  • {  move o so it is the last object in the module
  • }

That loop works only on one object at all, since when the loop goes back and tries to find the "next" object, there isn't one since the 1st object is now the last one in the module.

This is true for (?all?) loops of the form "for SomethingA in SomethingB" like "for link in object".

You need to 1st stage the objects in a skip list, THEN start moving them around.

==================================

  • Skip skpObjects = create() // KEY and DATA both 'Object'
  • for o in m do
  • {  put(skpObjects, o, o)
  • }
  • for o in skpObjects do
  • {  proceed with your algorithm
  • }

In your case you may have other problems since you are moving an object after another object, and I wonder what happens if you subsequently move THAT object.

-Louie

Hi Louie,

Thanks for responding. I understand the loop thing that you mentioned, and I have the code "o."Code Moved" = "Yes"" which marks each object that the code moves. The "if statement" in the beginning of the loop checks to see if attribute code moved is empty, only then it touches that object. This was the only way I could think of, since moving the object last does not do the intended job.

Re: Arrange objects based on list
pommCannelle - Mon Sep 28 12:00:01 EDT 2015

DOORS_user - Mon Sep 28 08:20:16 EDT 2015

Hi Louie,

Thanks for responding. I understand the loop thing that you mentioned, and I have the code "o."Code Moved" = "Yes"" which marks each object that the code moves. The "if statement" in the beginning of the loop checks to see if attribute code moved is empty, only then it touches that object. This was the only way I could think of, since moving the object last does not do the intended job.

Hmm ... The point of view of master Louie is formally  the good one.

In the other hand, to add a test on the 'code moved' attribute just after the loop statment is enough to ensure that the job will be done properly. 
Well, you are going to loop a little more than you really need to, but you don't need a skip anymore ... 
I have to confess that is what i usually use myself due to a maintainability purpose. 
This way is more ... 'readable' for a DXL beginner, although i agree that it's not the method to use. 

I take this opportunity to ask: Am i the only one to produce some ... 'not best' DXL in order to allow my users to modify my scripts by themselves ?

To close this topic, seems that the 2 ways are working ... it depends how much you're confortable with these algos ;)

 

Re: Arrange objects based on list
DOORS_user - Mon Sep 28 12:06:54 EDT 2015

Thank you everyone for responding. I made a few modifications and it seems to be working now. 

Re: Arrange objects based on list
llandale - Wed Sep 30 09:47:26 EDT 2015

llandale - Sat Sep 26 17:41:12 EDT 2015

Boring preamble: =================

Every programmer knows not to do this, as is screws up the loop.

  • for I in whatever
  • {  sometimes modify I
  • }

You are doing the same thing when you modify the order of objects from within the obj loop.  Consider the following:

  • for o in m do
  • {  move o so it is the last object in the module
  • }

That loop works only on one object at all, since when the loop goes back and tries to find the "next" object, there isn't one since the 1st object is now the last one in the module.

This is true for (?all?) loops of the form "for SomethingA in SomethingB" like "for link in object".

You need to 1st stage the objects in a skip list, THEN start moving them around.

==================================

  • Skip skpObjects = create() // KEY and DATA both 'Object'
  • for o in m do
  • {  put(skpObjects, o, o)
  • }
  • for o in skpObjects do
  • {  proceed with your algorithm
  • }

In your case you may have other problems since you are moving an object after another object, and I wonder what happens if you subsequently move THAT object.

-Louie

Since you are moving objects "after" and may subsequently move that other object, I wonder if you need to do this:

  • bool Done = false
  • while (!Done)
  • {  for o in m do
  •    {  Done = true  // unless some move is made below
  •        if (an object was moved) then Done = false
  •    } // end for each o in m
  • }   // end while !Done

This keeps moving objects until no more moves are needed.

I'm also guessing there is a more effective algorithm.  Perhaps before moving an object get an ordered list of all it's Sibling objects that come after it which have "Code Moved" as "Yes"; move each of these "after" after moving such an object.